/**
 * jims
 */
package com.jims.master.modules.reg.web;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.reg.entity.ClinicForRegist;
import com.jims.master.modules.reg.utils.DateUtil;
import com.jims.master.modules.sys.utils.UserUtils;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.reg.entity.ClinicAppoints;
import com.jims.master.modules.reg.service.ClinicAppointsService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约挂号Controller
 * @author 赵宁
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/reg/clinicAppoints")
public class ClinicAppointsController extends BaseController {

	@Autowired
	private ClinicAppointsService clinicAppointsService;
	
	@ModelAttribute
	public ClinicAppoints get(@RequestParam(required=false) String id) {
		ClinicAppoints entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clinicAppointsService.get(id);
		}
		if (entity == null){
			entity = new ClinicAppoints();
		}
		return entity;
	}

	/**
	 * 预约挂号页面
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list( Model model,ClinicForRegist clinicRegist) {
		String clinicName=clinicRegist.getClinicLabel();
		String deptId = clinicRegist.getDeptId();
		String date ="";
		if(clinicRegist.getClinicDate()!=null && !"".equals(clinicRegist.getClinicDate())){
			date= DateUtils.formatDateTime(clinicRegist.getClinicDate());
		}else{
			date= DateUtils.getTormmorow();
			clinicRegist.setClinicDate(DateUtils.parseDate(date));
		}
		List<ClinicForRegist> clinicForRegists = clinicAppointsService.findListRegist(date,clinicName,deptId);
		model.addAttribute("list", clinicForRegists);
		model.addAttribute("regist",clinicRegist);
		return "modules/reg/clinicAppointsIndex";
	}

	/**
	 * 预约确认列表
	 * @param model
	 * @param clinicAppoints
	 * @param request
	 * @param response
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"verifyIndex", ""})
	public String verifyList( Model model,ClinicAppoints clinicAppoints,HttpServletRequest request,HttpServletResponse response) {
        if(clinicAppoints.getVisitDate()==null || "".equals(clinicAppoints.getVisitDate())){
          clinicAppoints.setVisitDate(DateUtils.getDate("yyyy-MM-dd"));
		}
		Page<ClinicAppoints>  page=clinicAppointsService.findPage(new Page<ClinicAppoints>(request,response),clinicAppoints);
		model.addAttribute("page",page);
		return "modules/reg/clinicVerifyIndex";
	}


	/**
	 * 保存预约数据
	 * @param clinicIds
	 * @param clinicAppoints
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(String clinicIds,ClinicAppoints clinicAppoints, Model model) {
		StringData data=new StringData();
		String orgId= UserUtils.getOrgId();
		clinicAppoints.setOrgId(orgId);
		int i=clinicAppointsService.save(clinicAppoints,clinicIds);
		if(i>0){
			data.setCode("success");
			data.setData("预约成功");
		}else{
			data.setCode("error");
			data.setData("预约失败");
		}
		return data;
	}

	/**
	 * 预约确认
	 * @param clinicAppoints
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "confirmAppoints")
	@ResponseBody
	public StringData form(ClinicAppoints clinicAppoints, Model model,String clinicCharge,String payType) {
		StringData data=new StringData();
        int i =  clinicAppointsService.confirmAppoints(clinicAppoints,clinicCharge,payType);
		if(i>0){
			data.setCode("success");
			data.setData("确认成功");
		}else{
			data.setCode("error");
			data.setData("确认失败");
		}
		return data;
	}
	/**
	 * 预约修改页面
	 * @param clinicAppoints
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "appointsEditPage")
    public String appointsEdit(ClinicAppoints clinicAppoints,Model model){
		model.addAttribute("clinicAppoints",clinicAppoints);
      return "modules/reg/appointsEdit";
	}

	/**
	 * 预约修改保存
	 * @param clinicAppoints
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "appointsSave")
	@ResponseBody
    public StringData appointsSave(ClinicAppoints clinicAppoints){
        StringData data =new StringData();
        int i= clinicAppointsService.appointSave(clinicAppoints);
		if(i>0){
			data.setCode("success");
			data.setData("预约修改成功");
		}else if(i==0){
			data.setCode("warning");
			data.setData("号表不存在");
		}else{
			data.setCode("error");
			data.setData("预约修改失败");
		}
		return data;
	}

	/**
	 * 预约退约
	 * @param clinicAppoints
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ClinicAppoints clinicAppoints) {
	    StringData data=new StringData();
		int i=clinicAppointsService.delete(clinicAppoints);
        if(i>0){
            data.setCode("success");
            data.setData("退约成功");
        }else{
            data.setCode("error");
            data.setData("退约失败");
        }
		return data;
	}

}